<?php
			function get_tab_name($str)
            {
              	//echo 'dsf:'.$str;
	            if(stristr( $str,"vietnam"))
              		return 'VietNam';
              	else if(stristr($str,"Lao"))
              		return 'Lao';
              	else if(stristr($str,"Cambodia"))
              		return 'Cambodia';              	
              	else 
              		return 'Others';	
             }
             
             function check_category($str)
            {
              	//echo 'dsf:'.$str;
	            if(stristr( $str,"vietnam"))
              		return true;
              	else if(stristr($str,"Lao"))
              		return true;
              	else if(stristr($str,"Cambodia"))
              		return true;              	
              	else 
              		return false;	
             }
             
function db_get_where_to_you_go()
{
	$mywhere = get_option("setting_destination_transport");
	$where = explode(",", $mywhere);
	return $where;
}             
##********************************TOURS********************************
function db_travel_in_party_of()
{
	$travel_in_party_of = array('1 Person','2 Person','3 Person','4 Person','5 Person','6 Person','7 Person','8 Person','9 Person','10 Person','11 Person','12 Person','13 Person','14 Person','15 Person','16 Person and Upto');
	return $travel_in_party_of;
}

##********************************TOURS********************************
function db_type_room_list()
{
	$type_room_list = array('Superior Single','Superior Double/Twin','Deluxe Single','Deluxe Double/Twin','Vip Single','Suite Room');
	return $type_room_list;
}

function db_get_key_type_room($value_key)
{
	$type_room_list = db_type_room_list();
	foreach ($type_room_list as $key=>$value) 
	{
		$key +=1;
		if($value==$value_key)
			return $key;
	}
}

function db_get_name_type_room($value_key)
{
	$type_room_list = db_type_room_list();
	foreach ($type_room_list as $key=>$value) 
	{
		$key +=1;
		if($key==$value_key)
			return $value;
	}
}

function db_select_option_tour_person($current,$max)
{
	$option = null;
	//$type = array("Standard","Superior","First Class","Deluxe");
	for($i=1;$i<=$max;$i++) 
	{
		if($i<$max)
		{
			if($current==$i)
			{
			 	$option.='<option selected="selected" value="'.$i.' Person">'.$i.' Person</option>';
			}	
			else 
			{
			 	$option.='<option value="'.$i.' Person">'.$i.' Person</option>';
			}
		}
		else 
		{
			$option.='<option value="up-to">'.$max.' Person and Up to</option>';
		}
	}
	echo $option;
}

function db_select_option_tour_type($current)
{
	$option = null;
	$type = db_get_list_tour_type();
	foreach ($type as $key=>$value) 
	{
		if($current==$value)
		{
		 	$option.='<option selected="selected" value="'.$key.'">'.$value.'</option>';
		}	
		else 
		{
		 	$option.='<option value="'.$key.'">'.$value.'</option>';
		}
	}
	echo $option;
}

function db_list_link_admin()
{
	$home = get_bloginfo('home');
	$list_link = array(
		'Editor Hotels'		 => 'edit.php',  // post_hotel
		'Editor Tours'		 => 'edit.php?post_type=tour',
		'Editor News Event'	 => 'edit.php?post_type=news-event',
		'Editor Restaurant'	 => 'edit.php?post_type=restaurant',
		'Editor Partner'	 => 'edit.php?post_type=partner',
		'Editor Media'		 => 'upload.php',
		'Editor Quảng cáo'   => 'edit.php?post_type=adv',
		'Editor Banner'		 => 'edit.php?post_type=banner',
		'Editor Slide Show'  => 'edit.php?post_type=slide-show',
		'Editor Testimonial' => 'edit.php?post_type=testimonial',
		'Editor Link'		 => 'link-manager.php',
		'Editor Pages'		 => 'edit.php?post_type=page',
		'Editor Comments'	 => 'edit-comments.php',
		'Editor Theme'		 => 'themes.php',
		'Editor Plugin'		 => 'plugins.php',
		'Editor User'		 => 'users.php',
		'Editor Setting'	 => 'options-general.php',
	
		'Admin MySetting'			=> TEMPLATEPATH.'/admins/index.php',
		'Admin Payment'				=> TEMPLATEPATH.'/incs/payments/paypal/payments.php',
		'Admin Booked Tours'		=> TEMPLATEPATH.'/orders/tour/tour-setting.php',
		'Admin Booked Hotels'		=> TEMPLATEPATH.'/orders/hotel/hotel-setting.php',
		'Admin Booked Travel Agent'	=> TEMPLATEPATH.'/orders/agents/travel-agent-setting.php',
		'Admin Booked Ticket'		=> TEMPLATEPATH.'/orders/ticket/ticket-setting.php',
		'Admin Booked Visa'			=> TEMPLATEPATH.'/orders/visas/visa-setting.php'
	);
	
	return $list_link;
}

##**********************************************************************************************
//=======================Cat noi dung boi cac the ===================================
function db_split_content_by_tag($content)
{
	$arr_content = array();	
	while ($content)
	{
		$pos_star = stripos($content,"<!--more-->");
		if($pos_star)
		{	
			$sub_content = substr($content,0,$pos_star);
			$content = substr($content, $pos_star+11);
			$arr_content[]= $sub_content;
		}
		else {			
			$arr_content[] = $content;	
			$content=null;
		}
	}
	
    return $arr_content;
    //print_r($arr_content);
}

function db_check_exists_value_in_array($Array,$key)
{
	if($Array)
	{
		foreach ($Array as $value) 
		{
			if($value==$key)
				return true;
		}
	}
	return  false;	
}

function db_check_exists_value_in_array2($Array,$key)  // check $key=>value
{
	foreach ($Array as $mykey=>$value) 
	{
		if($mykey==$key)
			return $value;
	}
	return  false;
	
}

function db_select_option_year_checkIn($current)
{
	$year = date('Y');	
	$option = null;
	for($i=$year;$i<=$year+5;$i++)
	{
		$value = $i;
		if($i<10)
			$value = '0'.$i;
		if($current==$i)
		{
		 	$option.='<option selected="selected" value="'.$value.'">'.$i.'</option>';
		}	
		else 
		{
		 	$option.='<option value="'.$value.'">'.$i.'</option>';
		}
	}
	echo $option;
}

function db_select_option_month($current)
{
	$arry_month = array("January","February","March","April","May","June","July","August","September","October","November","December");
	$option = null;
	for($i=1;$i<=12;$i++)
	{
		$value = $i;
		if($i<10)
			$value = '0'.$i;
		if($current==$i)
		{
		 	$option.='<option selected="selected" value="'.$value.'">'.$arry_month[$i-1].'</option>';
		}	
		else 
		{
		 	$option.='<option value="'.$value.'">'.$arry_month[$i-1].'</option>';
		}
	}
	echo $option;
}


function db_select_option_date($current)
{
	$option = null;
	for($i=1;$i<=31;$i++)
	{
		$value = $i;
		if($i<10)
			$value = '0'.$i;
		if($current==$i)
		{
		 	$option.='<option selected="selected" value="'.$value.'">'.$i.'</option>';
		}	
		else 
		{
		 	$option.='<option value="'.$value.'">'.$i.'</option>';
		}
	}
	echo $option;
}	

	function db_select_all_datas($table)
	{
			global $wpdb;
			$table = $wpdb->prefix.$table;
			$query	= "SELECT * FROM $table";
			$result	= $wpdb->get_results($query, OBJECT);
			if($result)
				return $result;
			else 
				return false;	
	}
	function db_resizeImage($image_rs,$thumbnail,$size_x,$size_y)
	{	/*		
		$upload = wp_upload_dir();
		$base_thumbnail = pathinfo($thumbnail);		
		$file_name = $base_thumbnail['basename'];		
		$pos_uploads = stripos($base_thumbnail['dirname'],"uploads");
		$subdir = substr($base_thumbnail['dirname'],$pos_uploads+7);
		
		$source_file = $upload['basedir'].$subdir.'/'.$file_name;
		$des_file 	 = $upload['basedir'].$subdir.'/resize_'.$file_name;		
		
		list($width, $height, $type, $attr) = getimagesize($source_file);		
		if($width-10 > $size_x && $height-10 > $size_y)
		{	
			if(!file_exists($des_file) && file_exists($source_file))
			{
				$image_rs->load($source_file);
			    $image_rs->resize($size_x,$size_y);
			    $image_rs->save($des_file);
			}	
			unset($thumbnail);
			$thumbnail = $base_thumbnail['dirname'].'/resize_'.$file_name;		
		}	*/					
		return $thumbnail;
	}

	function db_get_destination_from()
	{
		$a = array();
		$des_from = db_select_all_datas("destination_from");
		if($des_from)
		{
			foreach ($des_from as $value) 
			{
				$a[$value->destinationFrom] = $value->destinationFrom;	
			}
		}		
		return $a;
	}
	
	function db_get_destination_to()
	{
		$a = array();
		$des_from = db_select_all_datas("destination_to");
		if($des_from)
		{
			foreach ($des_from as $value) 
			{
				$a[$value->destinationTo] = $value->destinationTo;	
			}
		}		
		return $a;
	}
	
	function bt_limit_chars($excerpt, $ilen =80)
	{
    			
    		if(strlen($excerpt)>$ilen){
    			$excerpt = substr($excerpt, 0,strpos($excerpt, ' ',$ilen)).'...';
    		}   		
    	
    		return $excerpt;
    }
	function is_post_type_url_request($type='')
	{
			$b= false;
			global $wp_query;
			if($wp_query->query_vars['post_type']==$type && empty($wp_query->query_vars['pagename'])&& !($wp_query->query_vars['page_id']) ){
				 $b = true;
			}
			return $b;
	}

	function db_get_mail_admin_managerment($option)
	{
		$admin_email = get_option("admin_email"); 
		$option_mail = get_option($option);
		if(strlen($option_mail)>0)
		{
			$admin_email = $option_mail;
		}
		return $admin_email;			
	}
	## filter theo taxonomy
	
	function pippin_add_taxonomy_filters() {
		global $typenow;
	 
		// an array of all the taxonomyies you want to display. Use the taxonomy name or slug
		$taxonomies = array('danh-muc-tin');
	 
		// must set this to the post type you want the filter(s) displayed on
		if( $typenow == 'tin-tuc' ){
	 
			foreach ($taxonomies as $tax_slug) {
				$tax_obj = get_taxonomy($tax_slug);
				$tax_name = $tax_obj->labels->name;
				$terms = get_terms($tax_slug);
				if(count($terms) > 0) {
					echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
					echo "<option value=''>Show All $tax_name</option>";
					foreach ($terms as $term) {
						echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
					}
					echo "</select>";
				}
			}
		}
	}
	add_action( 'restrict_manage_posts', 'pippin_add_taxonomy_filters' );


	## begin my function
	
	##==========================================================================================
	function db_get_list_post_in_post_type_nopage_metakey_post__not_in($posts_per_page,$post_type,$orderby,$order,$meta_key,$meta_value,$postID)
	{
		$args = array(
			'post_type'				=> $post_type,
			'posts_per_page'		=> $posts_per_page,
			'orderby'				=> $orderby,
			'post__not_in'			=> array($postID),
			'order'					=> $order,
			'publish_post'			=> 'publish',
			'meta_key'				=> $meta_key,
			'meta_value'			=> $meta_value
		);
		return $args;
	}
	
	function db_get_list_post_in_multiple_metakey_matevalue_nopage($post_type,$posts_per_page,$meta_key1,$meta_value1,$meta_key2,$meta_value2)
	{
		global $wpdb;
		$args = array(
			'post_type' 			=> $post_type,
			'posts_per_page'		=> $posts_per_page,
			'publish_post'			=> 'publish',
			'meta_query' => array(
				array(
					'key' 			=> $meta_key1,
					'value' 		=> $meta_value1					
				),
				array(
					'key' 			=> $meta_key2,
					'value' 		=> $meta_value2					
				)
			)
		 );
		 return $args;
	}
	
	function db_get_list_post_in_multiple_metakey_matevalue_3key_nopage($post_type, $posts_per_page, $meta_key1, $meta_value1, $meta_key2, $meta_value2, $meta_key3, $meta_value3)
	{
		global $wpdb;
		$args = array(
			'post_type' 			=> $post_type,
			'posts_per_page'		=> $posts_per_page,
			'publish_post'			=> 'publish',
			'meta_query' => array(
				array(
					'key' 			=> $meta_key1,
					'value' 		=> $meta_value1					
				),
				array(
					'key' 			=> $meta_key2,
					'value' 		=> $meta_value2					
				)
				,
				array(
					'key' 			=> $meta_key3,
					'value' 		=> $meta_value3					
				)
			)
		 );
		 return $args;
	}
	##==========================================================================================
	function db_get_list_post_in_post_type_page_metakey($posts_per_page,$post_type,$orderby,$order,$meta_key,$meta_value,$paged)
	{
		$args = array(
			'post_type'				=> $post_type,
			'posts_per_page'		=> $posts_per_page,
			'orderby'				=> $orderby,
			'order'					=> $order,
			'paged'					=> $paged,
			'publish_post'			=> 'publish',
			'meta_key'				=> $meta_key,
			'meta_value'			=> $meta_value
		);
		return $args;
	}
	##==========================================================================================
	function db_get_list_post_in_post_type_nopage_metakey($posts_per_page,$post_type,$orderby,$order,$meta_key,$meta_value)
	{
		$args = array(
			'post_type'				=> $post_type,
			'posts_per_page'		=> $posts_per_page,
			'orderby'				=> $orderby,
			'order'					=> $order,
			'publish_post'			=> 'publish',
			'meta_key'				=> $meta_key,
			'meta_value'			=> $meta_value
		);
		return $args;
	}
	

	##==========================================================================================
	function SearchFilter($query) 
	{
		$search = $_GET['search_current_page'];
		//print_r($search);
		if ($query->is_search) 
		{
		    // Insert the specific post type you want to search
		   // $query->set('post_type', 'post');
			if($search=="tours")
			{
				$query->set('post_type', 'tour');
			}	
			if($search=="hotels")
			{
				$query->set('post_type', 'post');
			}
			else 
			{		
				$query->set('post_type', 'news-event');
			}
		}		
	  	return $query;
	}	 
	// This filter will jump into the loop and arrange our results before they're returned
	add_filter('pre_get_posts','SearchFilter');	

	function db_get_tax_field_image($term_id)
	{	
		global $wpdb;			
	    $meta = get_option('additional_taxonomy_thumbnails');	    	    
	    if (empty($meta)) $meta = array();
	    if (!is_array($meta)) $meta = (array) $meta;
	     
	    $meta = isset($meta[$term_id]) ? $meta[$term_id] : array();	    
	    $images = $meta[$wpdb->prefix.'taxonomy_thumbnail'];    
	    if($images)
	    {
		    foreach ($images as $att) 
		    {
		    // get image's source based on size, can be 'thumbnail', 'medium', 'large', 'full' or registed post thumbnails sizes
		    $src = wp_get_attachment_image_src($att, 'full');
		    $src = $src[0];		     
		    // show image
		    return $src;
		    }
	    }
	}

	function db_get_tax_field($term_id)
	{		
		global $wpdb;
		$metas = get_option("additional_taxonomy_baseprice");
				
		if (!is_array($metas)) $metas = (array) $metas;
		$meta = isset($metas[$term_id]) ? $metas[$term_id] : array();
		
		if (!is_array($meta)) $meta = (array) $meta;
				
		//$meta = isset($meta[$term_id]) ? $meta[$term_id] : array();
		$value = $meta[$wpdb->prefix.'baseprice'];			
		return $value;
	}

	function db_query_post_multiple_meta_key()
	{
		global $wpdb;
		$args = array(
			'post_type' 			=> 'san-pham',
			'posts_per_page'		=> 5,
			'publish_post'			=> 'publish',
			'meta_query' => array(
				array(
					'key' 			=> $wpdb->prefix.'display',
					'value' 		=> 'display'					
				),
				array(
					'key' 			=> $wpdb->prefix.'slideshow',
					'value' 		=> 'display'					
				)
			)
		 );
		 return $args;
	}
	
	function db_query_post_in_posttype_2meta_key_paged($post_per_page,$post_type,$order_by,$order,$meta_key1,$meta_value1,$meta_key2,$meta_value2,$paged)
	{
		global $wpdb;
		$args = array(
			'post_type' 			=> $post_type,
			'posts_per_page'		=> $post_per_page,
			'publish_post'			=> 'publish',
			'orderby'				=> $order_by,
			'order'					=> $order,
			'paged'					=> $paged,
			'meta_query' => array(
				array(
					'key' 			=> $meta_key1,
					'value' 		=> $meta_value1					
				),
				array(
					'key' 			=> $meta_key2,
					'value' 		=> $meta_value2					
				)
			)
		 );
		 return $args;
	}
	function db_query_post_in_posttype_2meta_key_nopaged($post_per_page,$post_type,$order_by,$order,$meta_key1,$meta_value1,$meta_key2,$meta_value2,$paged)
	{
		global $wpdb;
		$args = array(
			'post_type' 			=> $post_type,
			'posts_per_page'		=> $post_per_page,
			'publish_post'			=> 'publish',
			'orderby'				=> $order_by,
			'order'					=> $order,
			'meta_query' => array(
				array(
					'key' 			=> $meta_key1,
					'value' 		=> $meta_value1					
				),
				array(
					'key' 			=> $meta_key2,
					'value' 		=> $meta_value2					
				)
			)
		 );
		 return $args;
	}
	##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

	function db_your_contact_us($_POST)
	{		
		$yourname 		 = trim($_POST['yourname']);
		$youremail 		 = trim($_POST['youremail']);		
		//$youraddress	 = trim($_POST['address']);
		//$yourphone	 = trim($_POST['phone']);
		$yoursubject	 = trim($_POST['yoursubject']);
		$yourmessage	 = trim($_POST['yourmessage']);	
		
		$yourcaptcha	 = $_POST['my_captcha'];		
		$mailto 		 = get_option("admin_email");		
		## delace prototype
		$db_session	= array();
		//$db_error	= array();
		$db_error	= null;
		
		if($yourname)			$db_session['yourname']		=	$yourname;
		if($youremail)			$db_session['youremail']		= 	$youremail;
		//if($youraddress)		$db_session['address']		= 	$youraddress;
		//if($yourphone)		$db_session['phone']		= 	$yourphone;
		if($yoursubject)		$db_session['yoursubject']	= 	$yoursubject;
		if($yourmessage)		$db_session['yourmessage']	= 	$yourmessage;
		if($yourcaptcha)		$db_session['yourcaptcha']		= 	$_POST['my_captcha'];
		
		$_SESSION['contact']	=	$db_session;
		
		//echo 'dfadsfasdfas:  '.$yourhidecode;
		//echo 'dfas:  '.$_POST['captcha'];
		
		if($yourcaptcha != $_POST['hide_code'])
		{
			$_SESSION['contact']['captcha']	= $yourcaptcha;
			$db_error = 'Nhập sai mã an toàn!';
		}
		if(!$db_error)
		{
				$param = array();
				$param['date']		= date('d-m-Y');
				$param['subject']	= "Liên hệ tại ".$_SERVER['HTTP_HOST'];
				$param['title']		= "Gửi phản hồi";
				$param['title_tag'] = "Form thông tin";
				
				$param['yourmessage']	= $yourmessage;
				$param['yourname']		= $yourname;
				$param['youremail']		= $youremail;
				//$param['phone']		= $yourphone;
				//$param['address']		= $youraddress;
				$param['yoursubject']	= $yoursubject;
				$param['emailto']	= $mailto;
				$param['thanks']	= "Cảm ơn bạn đã liên hệ với chúng tôi!";
				$param['fail']		= "Gửi mail thất bại!"; 
				
				//send email to management				
				$error =  db_send_email_to($param,$db_error);
				$db_error = $error;		  	  	
		}	
		return $db_error;	
	}

	function db_send_email_to($param)
	{
			$db_error='';	
			$subject = $param['subject'];
			$message = '
				<html>
				<head>
				  <title>'.$param['title'].'</title>
				</head>
				<body>
				  <p><h1>'.$param['title_tag'].'</h1></p>
				  <ul>
					<li>Name		 		: '.$param['fullname'].'</li>		
					<li>Email				: '.$param['email'].'</li>
					<li>Address				: '.$param['address'].'
					<li>Phone				: '.$param['phone'].'</li>																																								
					<li>Date 				: '.$param['date'].'</li>
					<li>Message				: '.$param['message'].'		  	
				  </ul>
				</body>
				</html>
			';
			$headers = array(
        	'From: '.$_SERVER[''].'',
        	"Content-Type: text/html"
  			);
  			$h = implode("\r\n",$headers) . "\r\n";
  			if(wp_mail($param['emailto'],$param['subject'],$message,$h))
			{
				$db_error 	.= $param['thanks'];
				$_SESSION['contact'] = null;
			}
			else 
			{
				$db_error 	.= $param['fail'];
				$_SESSION['contact'] = null;
			}
			
			return $db_error;
	}
	
	function db_day_mon_year_onday($date)
	{
		$timenow = gmdate("d / m / Y",strtotime($date));				
		return $timenow;
	}
	//=============================================================================================
	function db_list_different_post_args($id,$num_post,$post_type,$category_name,$orderby,$order,$metakey,$metavalue)
	{						
			$args = array(
				'post_type'			=> $post_type,		
				'posts_per_page'	 => $num_post,
				'post__not_in'		=> array($id),			
				'category_name'		=> $category_name,
				'orderby'			=> $orderby,			
				'order'				=> $order,
				'meta_key'				=> $metakey,
            	'meta_value'			=> $metavalue
			);			
			return $args;
	}
	
	function db_list_different_post_type($id,$num_post,$post_type,$orderby,$order,$metakey,$metavalue)
	{						
			$args = array(
				'post_type'			=> $post_type,		
				'posts_per_page'	 => $num_post,
				'post__not_in'		=> array($id),			
				'orderby'			=> $orderby,			
				'order'				=> $order,
				'meta_key'				=> $metakey,
            	'meta_value'			=> $metavalue
			);			
			return $args;
	}
	
	function db_list_different_post_type_child_term($id,$num_post,$post_type,$orderby,$order,$metakey,$metavalue,$taxonomy,$field,$term)
	{						
			$args = array(
				'post_type'			=> $post_type,		
				'posts_per_page'	 => $num_post,
				'post__not_in'		=> array($id),			
				'orderby'			=> $orderby,			
				'order'				=> $order,
				'meta_key'				=> $metakey,
            	'meta_value'			=> $metavalue,
				'tax_query' => array(
			     array(
			            'taxonomy' 	=> $taxonomy,
			        	'field'    	=> $field,          
			            'terms' 	=> array( $term ),
			            'operator' 	=> 'IN'
					        )
					    )
				
			);			
			return $args;
	}
	/*
	//=============================================================================================
	function ok_add_comment($post_id, $author, $author_email, $content, $user_id)
	{
		//echo $post_id.' - '.$author.' - '.$author_email.' - '.$content.' - '.$user_id;
			$time = current_time('mysql');
			$data = array(
			    'comment_post_ID' => $post_id,
			    'comment_author' => $author,
			    'comment_author_email' => $author_email,
			    'comment_author_url' => 'http://',
			    'comment_content' => $content,		    
			    'comment_parent' => 0,
			    'user_id' => $user_id,
			    'comment_author_IP' => preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] ),
			    'comment_agent' => substr($_SERVER['HTTP_USER_AGENT'], 0, 254),
			    'comment_date' => $time,
			    'comment_approved' => 0,
			);		
			$comments_id = wp_insert_comment($data);			
	}
	*/
	##============================================================================================
	
	##============================================================================================
	function db_query_all_post_in_post_type($num_post,$post_type,$orderby,$order,$meta_key,$meta_value)
	{
		$args = array(
			'post_type'				=> $post_type,
			'posts_per_page'		=> $num_post,
			'publish_post'			=> 'publish',			
			'meta_key'				=> $meta_key,
			'meta_value'			=> $meta_value,
			'orderby'				=> $orderby,
			'order'					=> $order
		);
		
		return $args;
	}
	##============================================================================================
	function db_query_post_random_in_post_type($num_post,$post_type,$orderby,$meta_key,$meta_value)
	{
		$args = array(
			'post_type'				=> $post_type,
			'posts_per_page'		=> $num_post,
			'publish_post'			=> 'publish',			
			'meta_key'				=> $meta_key,
			'meta_value'			=> $meta_value,
			'orderby'				=> $orderby			
		);
		
		return $args;
	}
	
	##============================================================================================
	function db_shortDesc($str, $len, $charset='UTF-8')
	{
		$str = html_entity_decode($str, ENT_QUOTES, $charset);
		if(mb_strlen($str)> $len)
		{
			$arr = explode(' ', $str);
			$str = substr($str, 0, $len);
			$arrRes = explode(' ', $str);
			$last = $arr[count($arrRes)-1];
			unset($arr);
			if(strcasecmp($arrRes[count($arrRes)-1], $last))
			{
				unset($arrRes[count($arrRes)-1]);
			}
			return implode(' ', $arrRes)."...";
		}		
		return $str;
	}
	##============================================================================================
	function db_query_posts_in_terms_child_nopage($num_post,$post_type,$taxonomy,$field,$term,$metakey,$metavalue)
	{
		$args = array(
	    'post_type' 		=> $post_type,
		'posts_per_page'	=> $num_post,				
		'meta_key'			=> $metakey,
		'meta_value'		=> $metavalue,
		'publish_post'		=> 'publish',
	    'tax_query' => array(
	     array(
	            'taxonomy' 	=> $taxonomy,
	        	'field'    	=> $field,          
	            'terms' 	=> array( $term ),
	            'operator' 	=> 'IN'
			        )
			    )
		);
		return $args;
	}
	##============================================================================================
	function db_query_posts_in_terms_child_paged($num_post,$post_type,$taxonomy,$field,$term,$metakey,$metavalue,$paged)
	{
		$args = array(
	    'post_type' 		=> $post_type,
		'posts_per_page'	=> $num_post,				
		'meta_key'			=> $metakey,
		'meta_value'		=> $metavalue,
		'paged'				=> $paged,
		'publish_post'		=> 'publish',
	    'tax_query' => array(
	     array(
	            'taxonomy' 	=> $taxonomy,
	        	'field'    	=> $field,          
	            'terms' 	=> array( $term ),
	            'operator' 	=> 'IN'
			        )
			    )
		);
		return $args;
	}
	
	##============================================================================================
	function db_query_posts_in_terms_child_nopage_exclude($num_post,$post_type,$taxonomy,$field,$term,$metakey,$metavalue)
	{
		$args = array(
	    'post_type' 		=> $post_type,
		'posts_per_page'	=> $num_post,				
		'meta_key'			=> $metakey,
		'meta_value'		=> $metavalue,
		'publish_post'		=> 'publish',
	    'tax_query' => array(
	     array(
	            'taxonomy' 	=> $taxonomy,
	        	'field'    	=> $field,          
	            'terms' 	=> $term,
	            'operator' 	=> 'NOT IN'
			        )
			    )
		);
		return $args;
	}
	
	##============================================================================================
	function db_query_posts_similar_in_terms_child_nopage($postID,$num_post,$post_type,$taxonomy,$field,$term,$metakey,$metavalue)
	{
		$args = array(
	    'post_type' 		=> $post_type,
		'posts_per_page'	=> $num_post,				
		'post__not_in'		=> array($postID),
		'meta_key'			=> $metakey,
		'meta_value'		=> $metavalue,
		'publish_post'		=> 'publish',
	    'tax_query' => array(
	     array(
	            'taxonomy' 	=> $taxonomy,
	        	'field'    	=> $field,          
	            'terms' 	=> array( $term ),
	            'operator' 	=> 'IN'
			        )
			    )
		);
		return $args;
	}
	
	##============================================================================================
	function db_query_posts_in_terms_child_multi_nopage($num_post,$post_type,$taxonomy,$field,$term,$metakey1,$metavalue1,$metakey2,$metavalue2)
	{
		$args = array(
	    'post_type' 		=> $post_type,
		'posts_per_page'	=> $num_post,				
		'meta_query' => array(
				array(
					'key' 			=> $metakey1,
					'value' 		=> $metavalue1					
				),
				array(
					'key' 			=> $metakey2,
					'value' 		=> $metavalue2					
				)
			),
		'publish_post'		=> 'publish',
	    'tax_query' => array(
	     array(
	            'taxonomy' 	=> $taxonomy,
	        	'field'    	=> $field,          
	            'terms' 	=> array( $term ),
	            'operator' 	=> 'IN'
			        )
			    )
		);
		return $args;
	}
	
	##============================================================================================
	function db_query_posts_in_terms_child_multi_paged($num_post,$post_type,$taxonomy,$field,$term,$metakey1,$metavalue1,$metakey2,$metavalue2,$paged)
	{
		$args = array(
	    'post_type' 		=> $post_type,
		'posts_per_page'	=> $num_post,	
		'paged'				=> $paged,		
		'meta_query' => array(
				array(
					'key' 			=> $metakey1,
					'value' 		=> $metavalue1					
				),
				array(
					'key' 			=> $metakey2,
					'value' 		=> $metavalue2					
				)
			),
		'publish_post'		=> 'publish',
	    'tax_query' => array(
	     array(
	            'taxonomy' 	=> $taxonomy,
	        	'field'    	=> $field,          
	            'terms' 	=> array( $term ),
	            'operator' 	=> 'IN'
			        )
			    )
		);
		return $args;
	}
	
	##============================================================================================
	function db_query_posts_in_terms_child_multi_3metakey_page($num_post,$post_type,$taxonomy,$field,$term,$aterm,$arayid,$metakey1,$metavalue1,$metakey2,$metavalue2,$metakey3,$metavalue3,$paged)
	{		
		
		if($arayid==0)   // term truyen vao la 1 taxonomy
		{
			//echo 'dafaaasfd: ';
			$args = array(
		    'post_type' 		=> $post_type,
			'posts_per_page'	=> $num_post,	
			'paged'				=> $paged,			
			'meta_query' => array(
					array(
						'key' 			=> $metakey1,
						'value' 		=> $metavalue1					
					),
					array(
						'key' 			=> $metakey2,
						'value' 		=> $metavalue2					
					),
					array(
						'key' 			=> $metakey3,
						'value' 		=> $metavalue3					
					)
				),
			'publish_post'		=> 'publish',
		    'tax_query' => array(
		     array(
		            'taxonomy' 	=> $taxonomy,
		        	'field'    	=> $field,          
		            'terms' 	=> array( $term ),
		            'operator' 	=> 'IN'
				        )
				    )
			);
		}
		else if($arayid==1)  // term truyen vao nhieu taxonomy
		{
			//echo 'fffffffffffffffff ';
			$args = array(
		    'post_type' 		=> $post_type,
			'posts_per_page'	=> $num_post,	
			'paged'				=> $paged,			
			'meta_query' => array(
					array(
						'key' 			=> $metakey1,
						'value' 		=> $metavalue1					
					),
					array(
						'key' 			=> $metakey2,
						'value' 		=> $metavalue2					
					),
					array(
						'key' 			=> $metakey3,
						'value' 		=> $metavalue3					
					)
				),
			'publish_post'		=> 'publish',
		    'tax_query' => array(
		     array(
		            'taxonomy' 	=> $taxonomy,
		        	'field'    	=> $field,          
		            'terms' 	=> $aterm,
		            'operator' 	=> 'IN'
				        )
				    )
			);
		}
		//print_r($args);
		return $args;
	}
	
	##============================================================================================
	function db_query_posts_in_terms_child_multi_2metakey_page($num_post,$post_type,$taxonomy,$field,$term,$aterm,$arayid,$metakey1,$metavalue1,$metakey2,$metavalue2,$paged)
	{		
		
		if($arayid==0)   // term truyen vao la 1 taxonomy
		{
			//echo 'dafaaasfd: ';
			$args = array(
		    'post_type' 		=> $post_type,
			'posts_per_page'	=> $num_post,	
			'paged'				=> $paged,			
			'meta_query' => array(
					array(
						'key' 			=> $metakey1,
						'value' 		=> $metavalue1					
					),
					array(
						'key' 			=> $metakey2,
						'value' 		=> $metavalue2					
					)
				),
			'publish_post'		=> 'publish',
		    'tax_query' => array(
		     array(
		            'taxonomy' 	=> $taxonomy,
		        	'field'    	=> $field,          
		            'terms' 	=> array( $term ),
		            'operator' 	=> 'IN'
				        )
				    )
			);
		}
		else if($arayid==1)  // term truyen vao nhieu taxonomy
		{
			//echo 'fffffffffffffffff ';
			$args = array(
		    'post_type' 		=> $post_type,
			'posts_per_page'	=> $num_post,	
			'paged'				=> $paged,			
			'meta_query' => array(
					array(
						'key' 			=> $metakey1,
						'value' 		=> $metavalue1					
					),
					array(
						'key' 			=> $metakey2,
						'value' 		=> $metavalue2					
					)
				),
			'publish_post'		=> 'publish',
		    'tax_query' => array(
		     array(
		            'taxonomy' 	=> $taxonomy,
		        	'field'    	=> $field,          
		            'terms' 	=> $aterm,
		            'operator' 	=> 'IN'
				        )
				    )
			);
		}
		//print_r($args);
		return $args;
	}
	
	##============================================================================================
	function db_query_posts_in_terms_child_multi_1metakey_page($num_post,$post_type,$taxonomy,$field,$term,$aterm,$arayid,$metakey1,$metavalue1,$paged)
	{		
		
		if($arayid==0)   // term truyen vao la 1 taxonomy
		{
			//echo 'dafaaasfd: ';
			$args = array(
		    'post_type' 		=> $post_type,
			'posts_per_page'	=> $num_post,	
			'paged'				=> $paged,			
			'meta_query' => array(
					array(
						'key' 			=> $metakey1,
						'value' 		=> $metavalue1					
					)
				),
			'publish_post'		=> 'publish',
		    'tax_query' => array(
		     array(
		            'taxonomy' 	=> $taxonomy,
		        	'field'    	=> $field,          
		            'terms' 	=> array( $term ),
		            'operator' 	=> 'IN'
				        )
				    )
			);
		}
		else if($arayid==1)  // term truyen vao nhieu taxonomy
		{
			//echo 'fffffffffffffffff ';
			$args = array(
		    'post_type' 		=> $post_type,
			'posts_per_page'	=> $num_post,	
			'paged'				=> $paged,			
			'meta_query' => array(
					array(
						'key' 			=> $metakey1,
						'value' 		=> $metavalue1					
					)
				),
			'publish_post'		=> 'publish',
		    'tax_query' => array(
		     array(
		            'taxonomy' 	=> $taxonomy,
		        	'field'    	=> $field,          
		            'terms' 	=> $aterm,
		            'operator' 	=> 'IN'
				        )
				    )
			);
		}
		//print_r($args);
		return $args;
	}
	
	##============================================================================================
	function db_query_posts_in_terms_child_multi_1taxonomy_page($num_post,$post_type,$taxonomy,$field,$term,$aterm,$arayid,$paged)
	{		
		
		if($arayid==0)   // term truyen vao la 1 taxonomy
		{
			//echo 'dafaaasfd: ';
			$args = array(
		    'post_type' 		=> $post_type,
			'posts_per_page'	=> $num_post,	
			'paged'				=> $paged,			
			'publish_post'		=> 'publish',
		    'tax_query' => array(
		     array(
		            'taxonomy' 	=> $taxonomy,
		        	'field'    	=> $field,          
		            'terms' 	=> array( $term ),
		            'operator' 	=> 'IN'
				        )
				    )
			);
		}
		else if($arayid==1)  // term truyen vao nhieu taxonomy
		{
			//echo 'fffffffffffffffff ';
			$args = array(
		    'post_type' 		=> $post_type,
			'posts_per_page'	=> $num_post,	
			'paged'				=> $paged,			
			'publish_post'		=> 'publish',
		    'tax_query' => array(
		     array(
		            'taxonomy' 	=> $taxonomy,
		        	'field'    	=> $field,          
		            'terms' 	=> $aterm,
		            'operator' 	=> 'IN'
				        )
				    )
			);
		}
		//print_r($args);
		return $args;
	}
	
	##============================================================================================
	function db_query_posts_in_terms_child_nopage_category_name($num_post,$post_type,$category_name,$metakey,$metavalue)
	{
		$args = array(
	    'post_type' 		=> $post_type,
		'posts_per_page'	=> $num_post,				
		'meta_key'			=> $metakey,
		'meta_value'		=> $metavalue,
		'publish_post'		=> 'publish',
	    'category_name'		=> $category_name
	    );
		return $args;
	}

	function db_get_first_images($content) 
	{	 
	  $first_img = '';
	  ob_start();
	  ob_end_clean();
	  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
	  $first_img = $matches [1] [0];
	
	  if(empty($first_img))
	  { //Defines a default image
	    return false;
	  }
	  else 
	  {
	  	return $first_img;
	  }
	}
	##======================= ATTACHMENT =================================================
		
	function db_get_alt_attachment_id($post_id,$meta_key)
	{
		$attachment_id = get_post_meta($post_id,$meta_key,true);
		$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
		if(count($alt))
			return $alt;
		else 
			return false;
	}
	
	function db_get_attached_file( $attachment_id, $unfiltered = false ) 
	{
		$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
		// If the file is relative, prepend upload dir				
		if($file)
		{
			$current_domain = $_SERVER['HTTP_HOST'];
			$site_url = get_option('siteurl');
			$new_domain = $site_url.'/wp-content/uploads/'.$file;	
			return $new_domain;
		}	
		return FALSE;	
	}

	function db_get_post_meta($post_id,$meta_key)
	{
		$attach_id = get_post_meta($post_id,$meta_key,true);
		$attach_file = db_get_attached_file($attach_id);	
		return $attach_file;
	}
	function db_get_post_meta_multi($post_id,$meta_key)
	{
		$multi_file = array();
		$attach_id = get_post_meta($post_id,$meta_key,false);
		if($attach_id)
		{
			foreach ($attach_id as $value) 
			{
				$multi_file[] = db_get_attached_file($value);
			}
		}
		return $multi_file;
	}
	##============================================================================================	

	function db_query_post_in_posttype_paged($post_per_page,$post_type,$meta_key,$meta_value,$paged)
	{
		$args = array(
			'post_type'			=> $post_type,
			'posts_per_page'	=> $post_per_page,
			'meta_key'          => $meta_key,		
			'meta_value'		=> $meta_value,
			'paged'				=> $paged,
			'publish_post'		=> 'publish' 
		);
		return $args;
	}
	
	##============================================================================================	
?>
<?php

	function db_get_name_for_filter($name)
	{
		$flag = null;
		$fields = array(
			'db_pattern'		=> 'Mẫu thiết kế',
			'db_project'		=> 'Dự án web'
		);
		
		foreach ($fields as $key=>$value)
		{
			if($key==$name)
				return $value;
		}
		return $flag;
	}
	
	##------------------------------------------------------------------------------------------
	function unix_to_gmt_sevent_vn() 
	{
	    $str_in = array ( "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",	"am", "pm",	":"    );
	    $str_out = array ( "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy", "Chủ nhật", " AM", " PM", " : ");
	    $time  = gmdate("D, d-m-Y g:i a", time() + 7*3600);
	    $time  = str_replace( $str_in, $str_out, $time);
	    return $time;
	}
	
	function unix_to_gmt_sevent_usa() 
	{
	    $str_in = array ( "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",	"am", "pm",	":"    );
	    $str_out = array ( "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy", "Chủ nhật", " : (AM)", " : (PM)", " : "    );
	    $time  = gmdate("d/m/Y g:i a", time() + 7*3600);
	   // $time  = str_replace( $str_in, $str_out, $time);
	    return $time;
	}
	
	function unix_to_gmt_sevent_usa2($time) 
	{
		$time = strtotime($time);
	    $str_in = array ( "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",	"am", "pm",	":"    );
	    $str_out = array ( "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy", "Chủ nhật", " : (AM)", " : (PM)", " : "    );
	     $time  = gmdate("d/m/Y g:i:s a", $time + 7*3600);
	   // $time  = str_replace( $str_in, $str_out, $time);
	    return $time;
	}
	
	function unix_to_gmt_sevent2($time) 
	{
		$time = strtotime($time);
	    $str_in = array ( "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",	"am", "pm",	":"    );
	    $str_out = array ( "Thứ hai", "Thứ ba", "Thứ tư", "Thứ năm", "Thứ sáu", "Thứ bảy", "Chủ nhật", " AM", " PM",":");
	    $time  = gmdate("d/m/Y g:i:s a", $time + 7*3600);
	    $time  = str_replace( $str_in, $str_out, $time);
	    return $time;
	}
?>